package com.research.web.controller;

import com.alibaba.druid.pool.DruidDataSource;
import com.research.web.entity.UserEntity;
import com.research.web.utils.UserSynchronizeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@Api(tags = "用户同步")
public class UserSynchronizeController {
    @Autowired
    private List<DruidDataSource> dataSourceList;

    @ApiOperation("用户手动同步所有数据信息")
    @RequestMapping(value = "/userManualSynchronize", method = RequestMethod.GET)
    public String userManualSynchronize() {
        try {
            UserSynchronizeUtils.synchronizeAllUserInfo(dataSourceList);
            return "同步成功";
        } catch (Exception e) {
            log.error("同步失败，原因为：" + e);
            return "同步失败";
        }
    }

    @ApiOperation("应用系统同步单条用户数据信息")
    @RequestMapping(value = "/serviceSynchronize", method = RequestMethod.POST)
    public String serviceSynchronize(UserEntity userEntity, String serviceId) {
        try {
//            boolean b = UserSynchronizeUtils.serviceSynchronize(dataSourceList, userMap);
            boolean b = UserSynchronizeUtils.serviceSynchronize(dataSourceList, userEntity, serviceId);
            if (b) {
                return "同步成功";
            } else {
                return "同步失败";
            }
        } catch (Exception e) {
            log.error("同步失败，原因为：" + e);
            return "同步失败";
        }
    }
}
